<template>
	<view class="container">
		<view class="navbar">
			<button type="default" plain class="talk-btn">
				<image src="/static/images/order/order_icon_talk2.0.png"></image>
				<view>想对你说</view>
			</button>
		</view>
		<view class="tabbar">
			<view class="item" :class="{active: !tabIndex}" @tap="switchTab(0)">当前订单</view>
			<view class="item" :class="{active: tabIndex}" @tap="switchTab(1)">历史订单</view>
		</view>
		<swiper :current="tabIndex" :duration="300" class="swiper" :show-scrollbar="true">
			<!-- 当前订单 begin -->
			<swiper-item @touchmove.stop="handleSwiperItemChange">
				<view v-if="currentOrders.length==0" class="no-order-content">
					<image src="https://go.cdn.heytea.com/storage/ad/2020/05/20/0bdb360866d94aa4a4404c0b676a1982.jpg">
					</image>
					<view class="tips">
						<view>您今天还没有下单</view>
						<view>快去选择一杯喜欢的茶吧</view>
					</view>
					<button type="primary" class="font-size-lg" hover-class="none">去下单</button>
				</view>
				<view class="current-order">
					<scroll-view scroll-y="true" class="scroll">
						<view class="order-list">
							<view class="order" v-for="order in currentOrders" :key="order.id">
								<navigator :url="'/pages/order/detail?id=' + order.id" open-type="navigate">
									<view class="header">
										<view class="flex-fill font-size-medium">订单类型：{{order.type==1? '自取':'外卖'}}
										</view>
										<view class="status">
											<view v-if='order.status==1'>未支付</view>
											<view v-else-if='order.status==2'>已支付</view>
											<view v-else-if='order.status==3'>{{order.type==1? '已完成':'派送中'}}</view>
											<view v-else-if='order.status==4'>已完成</view>
											<image src="/static/images/common/black_arrow_right.png"></image>
										</view>
									</view>
									<scroll-view scroll-x>
										<view class="images">
											<image :src="$img+item.image" v-for="(item, index) in order.details"
												:key="index">
											</image>
										</view>
									</scroll-view>
									<view class="info">
										<view class="left">
											<view v-if="order.status==2">取餐号：{{ order.takeCode }}</view>
											<view>下单时间：{{ order.createTime }}</view>
										</view>
										<view class="right">
											￥{{ order.amount }}
										</view>
									</view>
								</navigator>
								<view class="action">
									<button v-if="order.status==1" @click="buy(order)" type="primary" plain
										hover-class="none">立即支付</button>
									<button v-else @click="buy(order)" type="primary" plain
										hover-class="none">再来一单</button>
									<navigator :url="'/pages/order/detail?id=' + order.id" open-type="navigate">
										<button @click="" type="info" plain hover-class="none">查看详细</button>
									</navigator>
								</view>
							</view>
						</view>
					</scroll-view>

				</view>
			</swiper-item>
			</swiper-item>
			<!-- 当前订单 end -->
			<!-- 历史订单 begin -->
			<swiper-item @touchmove.stop="handleSwiperItemChange">
				<view class="history-order">
					<view class="menu">
						<view class="flex-fill d-flex justify-content-start">
							<view class="item" :class="{active: !orderMenuIndex}" @tap="switchMenuTab(0)">门店订单</view>
							<view class="item" :class="{active: orderMenuIndex}" @tap="switchMenuTab(1)">百货订单</view>
						</view>
						<view class="item" v-show="batchInvoiceVisible">
							<image src="/static/images/order/batch_invoice_icon.png"></image>
							<view>批量开票</view>
						</view>
					</view>
					<swiper :current="orderMenuIndex" :duration="300" :show-scrollbar="false"
						class="history-order-swiper">
						<!-- 门店订单 begin -->
						<swiper-item @touchmove.stop="handleSwiperItemChange">
							<scroll-view scroll-y="true" class="orders-scroll">
								<view class="wrapper">
									<view class="order-list">
										<navigator class="order" v-for="(order, index) in orders" :key="index"
											open-type="navigate" :url="'/pages/order/detail?id=' + order.id">
											<view class="header">
												<view class="flex-fill font-size-medium">{{ order.shop.name }}</view>
												<view class="status">
													<view>未支付</view>
													<image src="/static/images/common/black_arrow_right.png"></image>
												</view>
											</view>
											<scroll-view scroll-x>
												<view class="images">
													<image :src="item.image" v-for="(item, index) in order.items"
														:key="index"></image>
												</view>
											</scroll-view>
											<view class="info">
												<view class="left">
													<view>订单编号：{{ order.no }}</view>
													<view>下单时间：{{ order.created_at }}</view>
												</view>
												<view class="right">
													￥{{ order.total_fee }}
												</view>
											</view>
											<view class="action">
												<button type="default" hover-class="none">开发票</button>
												<button type="default" hover-class="none">查看评论</button>
												<button type="primary" plain hover-class="none">再来一单</button>
											</view>
										</navigator>
									</view>
								</view>
							</scroll-view>
						</swiper-item>
						<!-- 门店订单 end -->
						<!-- 百货订单 begin -->
						<swiper-item @touchmove.stop="handleSwiperItemChange">
							<view class="store-order-wrapper">
								<image src="/static/images/order/default_img_order.png"></image>
								<view>您还没有下单</view>
								<view>快去百货商城逛逛吧</view>
							</view>
						</swiper-item>
						<!-- 百货订单 end -->
					</swiper>
				</view>
			</swiper-item>
			<!-- 历史订单 end -->
		</swiper>
		<!-- <image src="https://go.cdn.heytea.com/storage/ad/2020/05/20/1a389117c2fb44d5bcad4a910a68246c.jpg"></image> -->
	</view>
</template>

<script>
	import order from "@/api/order.js"
	import pays from "@/api/pays.js";

	export default {
		data() {
			return {
				currentOrders: [],
				tabIndex: 0,
				orderMenuIndex: 0,
				orders: [],
				storeOrders: []
			}
		},
		async onLoad() {
			// order.getOrders().then(res => {
			// 	let now = Date.now()
			// 	res.data.forEach(item => {
			// 		let surplus = (now - Date.parse(item.endTime)) / 1000
			// 		this.$set(item, 'surMinute', parseInt(surplus / 3600))
			// 		this.$set(item, 'surSecond', parseInt(surplus % 3600 / 60))
			// 	})
			// 	this.currentOrders = res.data
			// })
			order.getOrders().then(res => {
				this.currentOrders = res.data
				console.log(res);
			})
		},
		computed: {
			batchInvoiceVisible() {
				return (!this.orderMenuIndex && this.orders.length) || (this.orderMenuIndex && this.storeOrders.length)
			}
		},
		methods: {
			buy(order) {
				pays.pay(order)
			},
			async switchTab(index) {
				if (this.tabIndex === index) return
				this.tabIndex = index
				if (this.tabIndex) {
					await this.getOrders()
				}
			},
			handleSwiperItemChange() { //禁止手动滑动
				return true
			},
			async switchMenuTab(index) {
				if (this.orderMenuIndex === index) return
				this.orderMenuIndex = index
				await this.getOrders()
			},
			async getOrders() {
				if (!this.orderMenuIndex) {
					this.orders = await this.$api('orders')
				} else {
					this.storeOrders = await this.$api('storeOrders')
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	page {
		background-color: #f6f6f6;
	}

	.navbar {
		height: calc(44px + var(--status-bar-height));
		/* #ifdef H5 */
		height: 44px;
		/* #endif */
		display: flex;
		background-color: #FFFFFF;
	}

	.talk-btn {
		height: 32px;
		margin-left: 10px;
		margin-top: 26px;
		/* #ifdef H5 */
		margin-top: 6px;
		/* #endif */
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: $font-size-sm !important;
		padding: 0 20rpx;
		border-radius: 50rem !important;

		image {
			width: 40rpx;
			height: 40rpx;
			margin-right: $spacing-row-sm;
		}
	}

	.tabbar {
		height: 100rpx;
		background-color: $bg-color-white;
		display: flex;
		align-items: center;
		justify-content: space-around;

		.item {
			height: 100%;
			font-size: $font-size-lg;
			color: $text-color-assist;
			font-weight: 400 !important;
			display: flex;
			align-items: center;

			&.active {
				color: $text-color-base;
				border-bottom: 4rpx solid $text-color-base;
			}
		}
	}

	.swiper {
		width: 100%;
		height: calc(100vh - 44px - var(--status-bar-height) - 110rpx);
		/* #ifdef H5 */
		height: calc(100vh - 44px - var(--status-bar-height) - 110rpx - 100rpx);
		/* #endif */
	}

	.no-order-content {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		image {
			width: 300rpx;
			height: 300rpx;
			margin-bottom: 50rpx;
		}

		.tips {
			color: $text-color-assist;
			display: flex;
			flex-direction: column;
			align-items: center;
			line-height: 1.2rem !important;
			margin-bottom: 70rpx;
		}

		button {
			width: 50%;
		}
	}

	.history-order {
		width: 100%;
		height: 100%;
		position: relative;

		.menu {
			padding: 18rpx 30rpx;
			display: flex;
			align-items: center;
			font-size: $font-size-base;
			color: $text-color-grey;
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			z-index: 10;

			.item {
				padding: 14rpx 30rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				image {
					width: 40rpx;
					height: 40rpx;
					margin-right: 10rpx;
				}

				&.active {
					color: $color-primary;
					background-color: $bg-color-white;
				}
			}
		}

		.history-order-swiper {
			width: 100%;
			height: 100%;
		}
	}

	.store-order-wrapper {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		font-size: $font-size-base;
		color: $text-color-assist;
		line-height: 1.3rem !important;

		image {
			width: 400rpx;
			height: 333rpx;
			margin-bottom: 40rpx;
		}
	}

	.orders-scroll {
		width: 100%;
		height: 100%;
		padding-top: 104rpx;
	}

	.order-list {
		display: flex;
		width: 100%;
		flex-direction: column;

		.order {
			background-color: $bg-color-white;
			padding: 30rpx 40rpx;
			margin-bottom: 18rpx;

			.header {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.status {
					font-size: $font-size-base;
					color: $text-color-grey;
					display: flex;
					align-items: center;

					image {
						width: 30rpx;
						height: 30rpx;
						margin-left: $spacing-row-sm;
					}
				}
			}

			.images {
				display: flex;
				column-gap: 0.5rem;
				padding: 30rpx 0;

				image {
					flex-shrink: 0;
					width: 150rpx;
					height: 112.5rpx;
				}
			}

			.info {
				display: flex;
				align-items: center;
				margin-bottom: 30rpx;

				.left {
					flex: 1;
					display: flex;
					flex-direction: column;
					font-size: $font-size-base;
					color: $text-color-grey;
				}

				.right {
					font-size: $font-size-lg;
					color: $text-color-base;
				}
			}

			.action {
				display: flex;
				justify-content: flex-end;
				align-items: center;

				button {
					margin-left: 30rpx;
					font-size: $font-size-sm;
				}
			}
		}
	}

	.current-order {
		height: 100%;

		.scroll {
			height: 100%;
		}
	}
</style>